Video Thumbnail
1:57
1:53
clock icon Created with Sketch. 1 minute

Solution: Inappropriate Intimacy


Roberto

why not doing something like this ?

@dataclass
class Geolocation:
id: int
street: str
postal_code: str
city: str
province: str
latitude: float
longitude: float
def __post_init__(self):
self.breadcrumbs: dict[str, str] = {}
main_url = "https://myapi.com"
self.breadcrumbs[
"postal_code_url"
] = f"{main_url}/postal_code/{self.postal_code}/"
city_slug = self.city.lower().replace(" ", "-")
self.breadcrumbs["city_url"] = f"{main_url}/region/{city_slug}/"
self.breadcrumbs[
"province_url"
] = f"{main_url}/region/province/{self.province.lower()}/"

REPLY
Arjan Egges

Hi Roberto, the issue with this approach is that now there is no way to generate breadcrumbs with anything that's not a Geolocation, and you also can't do it separately anymore from creating the Geolocation object. If you create a Geolocation object, and then change the postal code, the breadcrumbs won't be updated. I much prefer keeping it as a separate function.

REPLY
Juan José Expósito González

My solution trying several things:

from dataclasses import dataclass, field
from datetime import datetime

@dataclass
class Geolocation:
id: int
street: str
postal_code: str
city: str
province: str
latitude: float
longitude: float

@dataclass
class Location:
id: int
message_id: str
raw_data: str
date: datetime
priority: int
geolocation: list[Geolocation] = field(default_factory=list)

@dataclass
class BreadCrumb:
postal_code: str
city: str
province: str

def generate_breadcrumbs(geolocation: Geolocation) -> dict[str, str]:
breadcrumbs: dict[str, str] = {}
main_url = "https://myapi.com"
if geolocation:
if geolocation.postal_code:
breadcrumbs[
"postal_code_url"
] = f"{main_url}/postal_code/{geolocation.postal_code}/"
if geolocation.city:
city_slug = geolocation.city.lower().replace(" ", "-")
breadcrumbs["city_url"] = f"{main_url}/region/{city_slug}/"
if geolocation.province:
breadcrumbs[
"province_url"
] = f"{main_url}/region/province/{geolocation.province.lower()}/"
return breadcrumbs

def generate_breadcrumbs_with(
postal_code: str, city: str, province: str
) -> dict[str, str]:
breadcrumbs: dict[str, str] = {}
main_url = "https://myapi.com"

if postal_code:
breadcrumbs["postal_code_url"] = f"{main_url}/postal_code/{postal_code}/"
if city:
city_slug = city.lower().replace(" ", "-")
breadcrumbs["city_url"] = f"{main_url}/region/{city_slug}/"
if province:
breadcrumbs["province_url"] = f"{main_url}/region/province/{province.lower()}/"
return breadcrumbs

def generate_with_breadcrumbs(breadcrumb: BreadCrumb) -> dict[str, str]:
breadcrumbs: dict[str, str] = {}
main_url = "https://myapi.com"

if breadcrumb.postal_code:
breadcrumbs[
"postal_code_url"
] = f"{main_url}/postal_code/{breadcrumb.postal_code}/"
if breadcrumb.city:
city_slug = breadcrumb.city.lower().replace(" ", "-")
breadcrumbs["city_url"] = f"{main_url}/region/{city_slug}/"
if breadcrumb.province:
breadcrumbs[
"province_url"
] = f"{main_url}/region/province/{breadcrumb.province.lower()}/"
return breadcrumbs

def main() -> None:
location = Location(
id=1,
message_id="123456",
raw_data="raw data",
date=datetime.now(),
priority=1,
geolocation=[
Geolocation(
id=1,
street="123 Main St",
postal_code="12345",
city="New York",
province="NY",
latitude=40.7128,
longitude=74.0060,
)
],
)
breadcrumbs = generate_breadcrumbs(location.geolocation[0])
postal_code = location.geolocation[0].postal_code
city = location.geolocation[0].city
province = location.geolocation[0].province
breadcrumb = BreadCrumb(postal_code, city, province)
other_breadcrumbs = generate_breadcrumbs_with(postal_code, city, province)
yet_another_bc = generate_with_breadcrumbs(breadcrumb)
print(breadcrumbs)
print(other_breadcrumbs)
print(yet_another_bc)

if __name__ == "__main__":
main()

REPLY
Michael Brittain

You could also move the breadcrumbs method to be a property of the Geolocation class, that way you don't need to know about the internals, via print(geolocation.breadcrumbs).

REPLY
Arjan Egges

Good suggestion!

REPLY
Michael Brittain

The interesting thing is that there always seems to be a trade off - on the one hand we reduce the implementation details that are exposed externally but on the other hand maybe we add too much responsibility to the Geolocation class which is more about locations and not breadcrumbs. That’s the hard part to know!

REPLY